// pages/publish/publish.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgSrc:[],
    hiddenImg:true,
    content:'',
    photo:[],
    i:0,
    ctype:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  //删除img
  deleImg:function(e) {
    var that = this
    var index = e.target.dataset.index;
      var images = that.data.imgSrc;
      images.splice(index, 1);
      that.setData({
        imgSrc: images
      });
  },
  addImg: function () {
    var that = this
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        if (tempFilePaths) {
          that.setData({
            hiddenImg:false
          })
        }
        console.log(tempFilePaths)
        that.setData({
          imgSrc: tempFilePaths
        });
        

      }
    })
  },
  onLoad: function (options) {
    var ctype = options.ctype;
    this.setData({
      ctype:ctype,
    });
  },
  bindTextAreaBlur:function (e) {
    console.log(e)
    var content = e.detail.value;
    this.setData({
      content:content
    });
  },
  publish:function (e) {
    var that = this;
    var imgSrc = that.data.imgSrc;
    this.upload();
    
  },

  upload:function (e){
    var that = this;
    var imgSrc = that.data.imgSrc;
    var i = that.data.i;
    var length = imgSrc.length;
    wx.uploadFile({
      url: app.d.ceshiUrl + '/Api/Community/uploadimg',
      filePath: imgSrc[i],
      name: 'data',
      success: function (res) {
        console.log('uploadImage success, res is:', res);
        that.setData({
          photo: that.data.photo.concat(res.data),
          i:that.data.i + 1,
        })
        if(that.data.i<length){
          that.upload();
        }else{
          that.save();
        }
      },
      
      fail: function ({ errMsg }) {
        console.log('uploadImage fail, errMsg is', errMsg)
        return false;
      }
    })
  },
  save:function (e) {
    var that = this;
    var imgSrc = that.data.imgSrc;
    wx.request({
      url: app.d.ceshiUrl + '/Api/Community/publish',
      method: "post",
      data: {
        uid: app.d.userId,
        content: that.data.content,
        photo: that.data.photo,
        ctype: that.data.ctype,
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        console.log(res)
        if (res.data.status == 1) {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          });
          setTimeout(function () {
            wx.navigateBack({
              
            })
          }, 2000)
        
        } else {
          wx.showToast({
            title: res.data.err,
            duration: 2000
          })
        }
      },
      fail: function (e) {
        wx.showToast({
          title: '网络异常！',
          duration: 30000
        });
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})